package org.nvframe.component.render;

import org.newdawn.slick.Image;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.event.eventtype.UpdateEvent;
import org.nvframe.event.eventtype.UpdateListener;
import org.nvframe.event.eventtype.RenderEvent;
import org.nvframe.event.eventtype.RenderListener;
import org.nvframe.exception.NVFrameException;
import org.nvframe.manager.ConfigManager;
import org.nvframe.manager.ResourceManager;
import org.nvframe.util.settings.SettingsObj;

/**
 * 
 * @author Nik Van Looy
 */
public class ImageRender extends AbstractRender implements UpdateListener, RenderListener {

	protected Image image;
	protected float direction;
	protected boolean relativeDirection;
	
	public ImageRender(String id, Entity owner, SettingsObj settings) throws NVFrameException {
		super(id, owner, settings);

		relativeDirection = settings.getBoolean("relativeDirection", true);
		direction = 0.0f;
		
		loadImage(settings);
		
		EventService.getInstance().addEventListener(this);
	}
	
	/**
	 * Get the configured Image resource id from Settings and 
	 * get a reference to the Image resource trough ResourceManager
	 * 
	 * @param settings The Settings
	 * @throws NVFrameException
	 */
	public void loadImage(SettingsObj settings) throws NVFrameException {
		String resource = settings.getString("image");
		
		try {
			image = ResourceManager.getInstance().getImage(resource);
			image.setCenterOfRotation(image.getWidth() / 2, image.getHeight() / 2);
		} catch (NVFrameException e) {
			throw new NVFrameException("cannot load image for [id:" + resource + "] ImageRender component");
		}
	}

	/**
	 * Get the direction of the Render 
	 * 
	 * @return Direction of the Render in degrees
	 */
	public float getDirection() {
		return direction;
	}
	
	/**
	 * Set the direction of the Render
	 * 
	 * @param direction The direction in degrees
	 */
	public void setDirection(float direction) {
		this.direction = direction;
	}
	
	public boolean isRelativeDirection() {
		return relativeDirection;
	}

	public void setRelativeDirection(boolean relativeDirection) {
		this.relativeDirection = relativeDirection;
	}
	
	@Override
	public void onUpdate(UpdateEvent event) {
		// update the position/rotation of the image by retreiving the 
		// x and y values from the Position state component
		Position position = (Position) owner.getComponent(Position.class);
		
		if(relativeDirection)
			image.setRotation(position.getDirection() + direction);
		else
			image.setRotation(direction);
	}

	@Override
	public void onRender(RenderEvent event) {
		Position position = (Position) owner.getComponent(Position.class);
		
		image.draw(position.getX() - image.getWidth() / 2, 
				position.getY() - image.getHeight() / 2, position.getScale());
		
		if(ConfigManager.getInstance().getActive("engine_entities_showid"))
			event.getGr().drawString("id: " + owner.getName(), 
					position.getX() - 20, 
					position.getY() - 30);
	}

}